package org.dk.service.Impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.dk.pojo.Cart;
import org.dk.pojo.TbItem;
import org.dk.pojo.TbOrderItem;
import org.dk.service.CartService;
import org.dk.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@org.springframework.stereotype.Service
public class CartServiceImpl implements CartService {

    @Reference
    private ItemService itemService;

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public List<Cart> addCart(List<Cart> cartlist,Long itemId,Integer num) {

        //1,根据itemId获取item商品
        TbItem tbItem = itemService.selectById(itemId);
        //2,获取商品商家
        String sellerId = tbItem.getSellerId();
        //3,根据商家的id判断原理购物车中是否存在商家
        Cart cart = findCartBySellerId(cartlist, sellerId);
        // 4,没有
        if(cart == null){
            cart = new Cart();
            //商家id
            cart.setSellerId(sellerId);
            List<TbOrderItem> OrderItemlist = new ArrayList<>();

            TbOrderItem orderItemList = new TbOrderItem();
            TbOrderItem tbOrderItem = orderItemList(tbItem, num);
            //放入list中
            OrderItemlist.add(tbOrderItem);
            //放入到cart中
            cart.setOrderItemlist(OrderItemlist);
            cartlist.add(cart);
        }else{
            //5,有
            TbOrderItem orderItemByItemId = findOrderItemByItemId(cart.getOrderItemlist(), itemId);
            if (orderItemByItemId == null){
                orderItemByItemId = orderItemList(tbItem, num);
                cart.getOrderItemlist().add(orderItemByItemId);
            }else{
                orderItemByItemId.setNum(orderItemByItemId.getNum() + num);
                orderItemByItemId.setTotalFee(new BigDecimal(orderItemByItemId.getPrice().doubleValue() * orderItemByItemId.getNum()));
                if (orderItemByItemId.getNum().intValue() <= 0){
                    cart.getOrderItemlist().remove(orderItemByItemId);
                }
                if(cart.getOrderItemlist().size() <= 0){
                    cartlist.remove(cart);
                }
            }
        }
        return cartlist;
    }

    @Override
    public void addCartListToRedis(List<Cart> cartlist, String username) {
        redisTemplate.boundHashOps("carList").put(username,cartlist);
    }

    @Override
    public List<Cart> findListCartFromRedis(String username) {
        List<Cart> carList = (List<Cart>)redisTemplate.boundHashOps("carList").get(username);
        if(carList == null){
            carList = new ArrayList<>();
        }
        return carList;
    }

    @Override//合并
    public List<Cart> merageCartListCookieRedis(List<Cart> cookielist, List<Cart> redislist) {
        for (Cart cart : cookielist) {
            List<TbOrderItem> orderItemlist = cart.getOrderItemlist();
            for (TbOrderItem tbOrderItem : orderItemlist) {
                redislist = addCart(redislist,tbOrderItem.getItemId(),tbOrderItem.getNum());
            }
        }
        return redislist;
    }

    @Override
    public List<Cart> getCartListFormRedis(String id) {
        List<Cart> cartList = (List<Cart>)redisTemplate.boundHashOps("carList").get(id);
        return cartList == null ? new ArrayList<>() : cartList;
    }

    //封装,根据商家id查询购物车对象
    private Cart findCartBySellerId(List<Cart> cartlist,String sellerId){
        Cart ct = null;
        for (Cart cart : cartlist) {
            if (Objects.equals(cart.getSellerId(), sellerId)) {
                ct = cart;
                return ct;
            }
        }
        return null;
    }

    public TbOrderItem findOrderItemByItemId(List<TbOrderItem> orderItemList,Long id){
        return orderItemList.stream().filter(orderItem -> Objects.equals(orderItem.getItemId(), id)).findFirst().orElse(null);
    }

    public TbOrderItem orderItemList(TbItem item,Integer num){
        TbOrderItem orderItem = new TbOrderItem();
        orderItem.setGoodsId(item.getGoodsId());
        orderItem.setItemId(item.getId());
        orderItem.setNum(num);
        orderItem.setPrice(item.getPrice());
        orderItem.setTitle(item.getTitle());
        orderItem.setPicPath(item.getImage());
        orderItem.setTotalFee(new BigDecimal(item.getPrice().doubleValue() * num));
        return orderItem;
    }
}
